﻿using System;
using System.Linq;
using Csla.Data;
using Csla;

namespace Accounting.Library.Backup
{
    public class BackupK_PhieuXuatKho : BackupService
    {
        public BackupK_PhieuXuatKho(object ChungTu) : base(ChungTu) { }

        public override void BackupData(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, BackupMode backupMode)
        {
            //Backup K_PhieuXuatKho data
            DataAccess.K_PhieuXuatKho parent = (DataAccess.K_PhieuXuatKho)ChungTu;

            DataAccess.K_PhieuXuatKho_Bak backUpData = new DataAccess.K_PhieuXuatKho_Bak();
            backUpData.ChungTuBanDauId = parent.Khoa;
            backUpData.Ma = parent.Ma;
            backUpData.NgayChungTu = parent.NgayChungTu;
            backUpData.NhaCungCapId = parent.NhaCungCapId;
            backUpData.NguoiNhan = parent.NguoiNhan;
            backUpData.TongTien = parent.TongTien;
            backUpData.DienGiai = parent.DienGiai;
            backUpData.LoaiChungTuId = parent.LoaiChungTuId;

            //Insert Log data
            if (backupMode == BackupMode.Edit)
                backUpData.ThaoTac = BusinessConfiguration.Configuration.ThaoTac_ChinhSua;
            else if (backupMode == BackupMode.Delete)
                backUpData.ThaoTac = BusinessConfiguration.Configuration.ThaoTac_Xoa;
            backUpData.NhanVienThaoTac = ((Authentication.CustomIdentity)ApplicationContext.User.Identity).ID;
            try
            {
                int lanThaoTac = (int)(from p in ctx.DataContext.K_PhieuXuatKho_Baks
                                       where (p.ChungTuBanDauId == parent.Khoa)
                                       select p.SoLanThaoTac).Max();
                backUpData.SoLanThaoTac = lanThaoTac + 1;
            }
            catch (Exception)
            {
                backUpData.SoLanThaoTac = 1;
            }
            ctx.DataContext.K_PhieuXuatKho_Baks.InsertOnSubmit(backUpData);
            ctx.DataContext.SubmitChanges();

            //Backup K_PhieuXuatKho_ChiTiet data
            foreach (DataAccess.K_PhieuXuatKho_ChiTiet child in parent.K_PhieuXuatKho_ChiTiets)
            {
                DataAccess.K_PhieuXuatKho_ChiTiet_Bak childBackUpdata = new DataAccess.K_PhieuXuatKho_ChiTiet_Bak();
                childBackUpdata.ChungTuGocId = backUpData.Khoa;
                childBackUpdata.Khoa = child.Khoa;
                childBackUpdata.HangHoaId = child.HangHoaId;
                childBackUpdata.DienGiai = child.DienGiai;
                childBackUpdata.KhoId = child.KhoId;
                childBackUpdata.DVTId = child.DVTId;
                childBackUpdata.SoLuong = child.SoLuong;
                childBackUpdata.DonGia = child.DonGia;
                childBackUpdata.ThanhTien = child.ThanhTien;
                childBackUpdata.DoiTuongTHCPId = child.DoiTuongTHCPId;
                childBackUpdata.HangHoa = child.HangHoa;
                ctx.DataContext.K_PhieuXuatKho_ChiTiet_Baks.InsertOnSubmit(childBackUpdata);
            }

            ctx.DataContext.SubmitChanges();
        }
    }
}